package com.cloudfun.campusshare.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.IdentityType;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.UserAuthDTO;
import com.cloudfun.campusshare.entity.UserAuthEntity;
import com.cloudfun.campusshare.repo.UserAuthRepo;
import com.cloudfun.campusshare.service.UserAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Huqin on 2020/4/16
 */
@Slf4j
@Service
public class UserAuthServiceImpl implements UserAuthService {

    @Autowired
    private UserAuthRepo userAuthRepo;

    @Transactional
    @Override
    public void create(UserAuthDTO userAuthDTO) {
        UserAuthEntity userAuthEntity = this.getByUserIdAndType(userAuthDTO.getUserId(), userAuthDTO.getIdentityType());
        if (null == userAuthEntity) {
            userAuthEntity = this.getByIdentityTypeAndIdentifier(userAuthDTO.getIdentityType(), userAuthDTO.getIdentifier());
            if (null == userAuthEntity) {
                userAuthEntity = new UserAuthEntity();
            }
        }
        CopyOptions copyOptions = CopyOptions.create();
        copyOptions.setIgnoreNullValue(true);
        BeanUtil.copyProperties(userAuthDTO, userAuthEntity, copyOptions);
        userAuthRepo.save(userAuthEntity);
    }

    @Override
    public String updateByIdentifier(UserAuthDTO userAuthDTO) {
        UserAuthEntity byIdentityTypeAndIdentifier = this.getByIdentityTypeAndIdentifier(userAuthDTO.getIdentityType(), userAuthDTO.getIdentifier());
        if (null != byIdentityTypeAndIdentifier && !byIdentityTypeAndIdentifier.getUserId().equals(userAuthDTO.getUserId())) {
            // 如果根据认证类型和认证标识能找到记录，且记录的userId与传进来的不一样，则返回错误
            throw new BusinessException(Code.DATA_EXIST, "认证信息已存在");
        }
        UserAuthEntity userAuthEntity = this.getByUserIdAndType(userAuthDTO.getUserId(), userAuthDTO.getIdentityType());
        if (null == userAuthEntity) {
            userAuthEntity = new UserAuthEntity();
        }
        CopyOptions copyOptions = CopyOptions.create();
        copyOptions.setIgnoreNullValue(true);
        BeanUtil.copyProperties(userAuthDTO, userAuthEntity, copyOptions);
        userAuthRepo.save(userAuthEntity);
        return userAuthEntity.getId();
    }

    @Override
    public UserAuthEntity getByUserIdAndType(String userId, IdentityType identityType) {
        return userAuthRepo.findByUserIdAndIdentityType(userId, identityType);
    }

    @Override
    public UserAuthEntity getByIdentityTypeAndIdentifier(IdentityType identityType, String identifier) {
        return userAuthRepo.findByIdentityTypeAndIdentifier(identityType, identifier);
    }

    @Override
    public void updateByIdentifier(UserAuthEntity userAuthEntity) {
        userAuthRepo.save(userAuthEntity);
    }

    @Override
    public void deleteByUserId(String userId) {
        List<UserAuthEntity> allByUserId = userAuthRepo.findAllByUserId(userId);
        userAuthRepo.deleteAll(allByUserId);
    }
}
